package com.wifiunion.zmkm.activity;

import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.wifiunion.zmkm.entity.AddressInfoEntity;
import com.wifiunion.zmkm.entity.User;
import com.wifiunion.zmkm.entity.UserAddressEntity;
import com.wifiunion.zmkm.utils.ZMKMUSER_BitmapUtils;
import com.wifiunion.zmkm.utils.ZMKMUSER_CommonUtils;
import com.wifiunion.zmkm.utils.ZMKMUSER_Constant;
import com.wifiunion.zmkm.utils.ZMKMUSER_HttpUtils;
import com.wifiunion.zmkm.utils.ZMKMUSER_SharePUtils;
import com.wifiunion.zmkmusersdk.R;

public class MainActivity extends Activity {

	private LinkedList<AddressInfoEntity> addressList = new LinkedList<AddressInfoEntity>();

	private LinkedList<UserAddressEntity> userAddressList = new LinkedList<UserAddressEntity>();
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == ZMKMUSER_HttpUtils.HTTP_NETERROR_CODE) {
			} else if (msg.what == ZMKMUSER_HttpUtils.HTTP_SUCCESS_CODE) {

				String result = (String) msg.obj;

				try {
					JSONObject jsonObj = new JSONObject(result);
					JSONArray jsonArray = jsonObj.getJSONArray("AddressInfo");

					addressList.clear();
					for (int i = 0; i < jsonArray.length(); i++) {

						JSONObject jsonSubObj = (JSONObject) jsonArray.get(i);

						AddressInfoEntity addressInfo = new AddressInfoEntity();

						addressInfo.setFmUuid(jsonSubObj.getString("fmUuid"));
						addressInfo.setProvice(jsonSubObj.getString("province"));
						addressInfo.setCity(jsonSubObj.getString("city"));
						addressInfo.setArea(jsonSubObj.getString("area"));
						addressInfo.setCommunityName(jsonSubObj.getString("communityName"));
						addressInfo.setSegmentName(jsonSubObj.getString("segmentName"));
						addressInfo.setBuildingName(jsonSubObj.getString("buildingName"));
						addressInfo.setUnitName(jsonSubObj.getString("unitName"));
						addressInfo.setRoomName(jsonSubObj.getString("roomName"));
						addressList.add(addressInfo);
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

				// ZMKMUSER_HttpUtils.CertRequest(MainActivity.this,
				// "202827e0-99fa-4fb3-9e6c-dc37ded624fb", certhandler);

			}
		}

	};

	private Handler certhandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == ZMKMUSER_HttpUtils.HTTP_SUCCESS_CODE) {
				Bitmap result = (Bitmap) msg.obj;
				if (result == null) {
				} else {
					qrcodeImg.setImageBitmap(result);
				}
			}
		}

	};

	private ImageView qrcodeImg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ZMKMUSER_SharePUtils.setSharedPreferences(ZMKMUSER_Constant.CONFIG_APPID, ZMKMUSER_Constant.APP_ID, this);
		qrcodeImg = (ImageView) findViewById(R.id.qrcode_img);
		Button btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ZMKMUSER_HttpUtils.LoginRequest(MainActivity.this, "18051964266", ZMKMUSER_Constant.APP_ID, handler);
			}
		});

		Button btn1 = (Button) findViewById(R.id.btn_1);

		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				ZMKMUSER_HttpUtils.qrCodeRequest(MainActivity.this, "2a1ac626-3a83-45ed-a2eb-9347162f8efd", certhandler);
				// UserAddressEntity address =
				// getUserAddressInfo("202827e0-99fa-4fb3-9e6c-dc37ded624fb");

				// if (address == null) {
				//
				// Toast.makeText(MainActivity.this, "出异常了1", 3000).show();
				// } else {
				// Bitmap bmp =
				// ZMKMUSER_BitmapUtils.getQrCodeBitmap(MainActivity.this,
				// address.getFmUuid());
				// if (bmp == null) {
				// Toast.makeText(MainActivity.this, "出异常了2", 3000).show();
				// } else {
				// qrcodeImg.setImageBitmap(bmp);
				// }
				// }
			}
		});
	}

	public UserAddressEntity getUserAddressInfo(String fmUuid) {
		User user = (User) ZMKMUSER_CommonUtils.readObject(MainActivity.this, ZMKMUSER_Constant.CONFIG_USER);
		if (user != null) {
			userAddressList = user.getAddresses();
			if (userAddressList != null) {
				if (userAddressList.size() > 0) {
					for (UserAddressEntity address : userAddressList) {
						if (fmUuid.equals(address.getFmUuid())) {
							return address;
						}
					}
				}
			}
		}

		return null;
	}

}
